package com.poi.demopoi.config.datasource.druid;

import com.alibaba.druid.filter.config.ConfigTools;

public class MtDruid {
    public static void main(String[] args) throws Exception {
        druidEncrypt();
    }

    /**
     *
     * > D:\Program_software\Professional\maven\repo\com\alibaba\druid\1.1.10
     * > java -cp druid-1.1.10.jar com.alibaba.druid.filter.config.ConfigTools root
     * privateKey:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAjPHU2ugEjTUH4gGhxqwyfcMUqVqJ6SkpVXqkW9gnGat5uepMF+0PXB1hvvfhfQJDKaq5Ll86B144HYfM/SEcwQIDAQABAkByCLdO5anJFb7q/KcHNwsi9VMiMj6ihssKfc+8/4WjHgUhDk3EtEr8sOznWblZUY+EAkVzDu81GyVcNc4VOJaBAiEAz87RZl+C2ZPjg9qQW7m5hoofJkraXUWOB+/uQN0HtbMCIQCtoXaa2j1UmW73k2qb6xdo78wbg8maJlcXCXiXCpeRuwIgf5q9BMLzg42HcKjeaiTCorXf6NCgBBQ44fDX+WaAXHcCIFBAZ8RtCJnYjQyLK2RDhb9vzmQNRpokDG7OZtlRAPtPAiArTDH9Y4ec68UNQr42ibYgyr5UuDTDoxlU0QUY1/J/jA==
     * publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIzx1NroBI01B+IBocasMn3DFKlaiekpKVV6pFvYJxmrebnqTBftD1wdYb734X0CQymquS5fOgdeOB2HzP0hHMECAwEAAQ==
     * password:PzSd7JytaJduw4kfW/Oktfr1e1ER5h8ueYqvucm4HukaH6+WY3bRL0yI9qlzCE68WoaVm+Z8Cgjc5Im5XwFbtw==
     *
     * druid:
     *  connect-properties:
     *      config.decrypt: true
     *      config.decrypt.key: ${public_key}
     *  filter:
     *      config:
     *        enabled: true   # 启动ConfigFilter 必须
     *
     * 明文密码: root
     * privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAl1L+T87ESaKlSBIfttsQcIIi+laTUUyKBaUuW03oLmm96baJ94WVU11Fu5RGTxFEWojXqjTqXdN2TyeeEIEKaQIDAQABAkApQmK+xeS1IxinrvFgvRgdZQ4+bY6bUyAq69/Y20QcKyK7KoOp49C8Ce6gPerDU3x8bOwJcId2lZ6o48D0B5m1AiEA22SQ+zOtdJovesxClMS+unJdFRdnCRz+ZYb5enaHT/sCIQCwktqIRPKbyjtvs4bjP++LnO/MDWqgnEqYt/xBqVqt6wIhAI51N7sVrEzpjWkEVzt4oYw5lH2Mi6gcPog4Y6iEbIqfAiEAhZLCID3QLcwhhFGR481VAA97U6h+lftFbxwXhvB/OXsCIQDOS6/TKE+hcP3KIcBmNYTl2221RykVPnD+IADCNfB81Q==
     * publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJdS/k/OxEmipUgSH7bbEHCCIvpWk1FMigWlLltN6C5pvem2ifeFlVNdRbuURk8RRFqI16o06l3Tdk8nnhCBCmkCAwEAAQ==
     * password:aSVbXM6GS2VfAZzWitwo86X//SIIJWOOIto4JQymTPrNFGz1Y3IaNvsE9J5G9LiaFy4ii70hyn5BUj7pSrBzSw==
     * 解密后:root
     *
     */
    public static void druidEncrypt() throws Exception {
        //密码明文
        String password = "root";
        System.out.println("明文密码: " + password);
        String[] keyPair = ConfigTools.genKeyPair(512);
        //私钥
        String privateKey = keyPair[0];
        //公钥
        String publicKey = keyPair[1];

        //用私钥加密后的密文
        password = ConfigTools.encrypt(privateKey, password);

        System.out.println("privateKey:" + privateKey);
        System.out.println("publicKey:" + publicKey);

        System.out.println("password:" + password);

        String decryptPassword = ConfigTools.decrypt(publicKey, password);
        System.out.println("解密后:" + decryptPassword);
    }

}
